// const WXAPI = require('apifm-wxapi')
import api from "../../api/index.js";
Page({
  data: {
    banners: [],
    user: {}
  },
  onLoad(e) {
    getApp().initLanguage(this)
    this.banners()
  },
  onShow() {
    let user = wx.getStorageSync('user')
    this.setData({
      user
    })
  },
  async processGotUserDetail(apiUserInfoMap) {
    if (!apiUserInfoMap) {
      return
    }
    this.setData({
      apiUserInfoMap,
      nick: apiUserInfoMap.base.nick
    })
  },
  async banners() {
    api.getSwiperImgs({
      data: {},
    }).then((res) => {
      this.setData({
        banners: res.data
      })
    })
  },
  tapBanner(e) {
    const url = e.currentTarget.dataset.url
    if (!url) {
      return;
    }
    const item = e.currentTarget.dataset.item
    //
    const pagePath = ["/pages/index/index", "/pages/order/index", "/pages/order-details/doing", "/pages/my/index"]
    if (pagePath.find(element => element === url.substr(0, element.length))) {
      //调整至tab页
      wx.switchTab({
        url,
        success: function (res) {
          // 通过eventChannel向被打开页面传送数据
          res.eventChannel.emit('acceptDataFromOpenerPage', {
            ...item
          })
        }
      })
    } else {
      wx.navigateTo({
        url,
        success: function (res) {
          // 通过eventChannel向被打开页面传送数据
          res.eventChannel.emit('acceptDataFromOpenerPage', {
            ...item
          })
        }
      })
    }
  },
  onShareAppMessage() {
    return {
      title: wx.getStorageSync('mallName') + ' ' + wx.getStorageSync('share_profile'),
      path: '/pages/home/index?inviter_id=' + (wx.getStorageSync('uid') || ''),
      imageUrl: wx.getStorageSync('share_pic')
    }
  },
  onShareTimeline() {
    return {
      title: wx.getStorageSync('mallName') + ' ' + wx.getStorageSync('share_profile'),
      query: 'inviter_id=' + (wx.getStorageSync('uid') || ''),
      imageUrl: wx.getStorageSync('share_pic')
    }
  },
  changeLang() {
    getApp().changeLang(this)
  },
  huiyuan() {
    wx.navigateTo({
      url: '/pages/member-center/index',
    })
  },
  coupon() {
    wx.navigateTo({
      url: '/pages/coupons/index',
    })
  },
  changePeisongType(e) {
    const peisongType = e.currentTarget.dataset.type
    wx.setStorageSync('peisongType', peisongType)
    wx.switchTab({
      url: '/pages/order/index',
    })
  },
  about() {
    wx.navigateTo({
      url: '/pages/about/index',
    })
  },
  toAboutPage(e) {
    const articleTitle = e.currentTarget.dataset.article
    api.getArticleByName({
      data: {
        "title":articleTitle
      },
    }).then((res) => {
      if (res.data) {
        this.toAboutFromArticle(articleTitle,res.data)
      }else{
          
      }
    })
  },
  card() {
    wx.navigateTo({
      url: '/pages/card/index',
    })
  },
  toAboutFromArticle(title,article){
    let param =  {
      title,//标题
      detailImg: article.contentImg,
      detailBtnName:"返回"//返回按钮
    }
    wx.navigateTo({
      url: '/pages/about/index?key=首页1',
      success: function (res) {
        // 通过eventChannel向被打开页面传送数据
        res.eventChannel.emit('acceptDataFromOpenerPage', param)
      }
    })
  }
})